/*
 * Profile.h
 *
 *  Created on: 2011-8-2
 *      Author: gexiao
 */

#ifndef PROFILE_H_
#define PROFILE_H_

#ifndef NOT_VM      /* for utilities that sneakily include this file */

#include <stdio.h>
/* External allocations are hackish enough that it's worthwhile
 * separating them for possible removal later.
 */
#define PROFILE_EXTERNAL_ALLOCATIONS 1
struct Thread; // extern

/*
 * Method trace state.  This is currently global.  In theory we could make
 * most of this per-thread.
 */
typedef struct MethodTraceState {
	/* these are set during VM init */
	Method* gcMethod;
	Method* classPrepMethod;

	/* active state */
	pthread_mutex_t startStopLock;
	pthread_cond_t threadExitCond;
	FILE* traceFile;
	bool directToDdms;
	int bufferSize;
	int flags;

	int traceEnabled;
	u1* buf;
	volatile int curOffset;
	u8 startWhen;
	int overflow;
} MethodTraceState;

/*
 * Memory allocation profiler state.  This is used both globally and
 * per-thread.
 *
 * If you add a field here, zero it out in dvmStartAllocCounting().
 */
typedef struct AllocProfState {
	bool enabled; // is allocation tracking enabled?

	int allocCount; // #of objects allocated
	int allocSize; // cumulative size of objects

	int failedAllocCount; // #of times an allocation failed
	int failedAllocSize; // cumulative size of failed allocations

	int freeCount; // #of objects freed
	int freeSize; // cumulative size of freed objects

	int gcCount; // #of times an allocation triggered a GC

	int classInitCount; // #of initialized classes
	u8 classInitTime; // cumulative time spent in class init (nsec)

#if PROFILE_EXTERNAL_ALLOCATIONS
	int externalAllocCount; // #of calls to dvmTrackExternalAllocation()
	int externalAllocSize; // #of bytes passed to ...ExternalAllocation()

	int failedExternalAllocCount; // #of times an allocation failed
	int failedExternalAllocSize; // cumulative size of failed allocations

	int externalFreeCount; // #of calls to dvmTrackExternalFree()
	int externalFreeSize; // #of bytes passed to ...ExternalFree()
#endif  // PROFILE_EXTERNAL_ALLOCATIONS
} AllocProfState;

/*
 * Call these when a method enters or exits.
 */
#define TRACE_METHOD_ENTER(_self, _method)                                 \
    do {                                                                    \
        if (gDvm.activeProfilers != 0) {                                    \
            if (gDvm.methodTrace.traceEnabled)                              \
                dvmMethodTraceAdd(_self, _method, METHOD_TRACE_ENTER);      \
            if (gDvm.emulatorTraceEnableCount != 0)                         \
                dvmEmitEmulatorTrace(_method, METHOD_TRACE_ENTER);          \
        }                                                                   \
    } while(0);
#define TRACE_METHOD_EXIT(_self, _method)                                  \
    do {                                                                    \
        if (gDvm.activeProfilers != 0) {                                    \
            if (gDvm.methodTrace.traceEnabled)                              \
                dvmMethodTraceAdd(_self, _method, METHOD_TRACE_EXIT);       \
            if (gDvm.emulatorTraceEnableCount != 0)                         \
                dvmEmitEmulatorTrace(_method, METHOD_TRACE_EXIT);           \
        }                                                                   \
    } while(0);

void dvmMethodTraceAdd(struct Thread* self, const Method* method, int action);
void dvmEmitEmulatorTrace(const Method* method, int action);
#endif

/*
 * Enumeration for the two "action" bits.
 */
enum {
	METHOD_TRACE_ENTER = 0x00, // method entry
	METHOD_TRACE_EXIT = 0x01, // method exit
	METHOD_TRACE_UNROLL = 0x02,
// method exited by exception unrolling
// 0x03 currently unused
};
#endif /* PROFILE_H_ */
